package cn.pzaily.orange.manage.service.impl;

import cn.pzaily.orange.core.utils.convert.ConvertUtils;
import cn.pzaily.orange.core.utils.page.PageRequest;
import cn.pzaily.orange.core.utils.page.PageResponse;
import cn.pzaily.orange.core.utils.StringUtils;
import cn.pzaily.orange.manage.constant.SysConstants;
import cn.pzaily.orange.manage.dao.SysMenuMapper;
import cn.pzaily.orange.manage.model.SysMenu;
import cn.pzaily.orange.manage.model.vo.SysMenuTreeNode;
import cn.pzaily.orange.manage.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: cn.pzaily.orange.manage.server.impl
 * @Description:
 * @Author: Pang
 * @Date: 2021-05-12 21:31
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Override
    public PageResponse findPage(PageRequest param) {
        return null;
    }

    @Override
    public List<SysMenu> findAll() {
        return null;
    }

    @Override
    public boolean save(SysMenu data) {
        return false;
    }

    @Override
    public boolean modify(SysMenu data) {
        return false;
    }

    @Override
    public SysMenu findById(Long id) {
        return null;
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        return false;
    }

    @Override
    public List<SysMenu> findByUserName(String name) {
        if (StringUtils.isNotEmpty(name) && SysConstants.ADMIN.equalsIgnoreCase(name)) {
            return menuMapper.selectByExample(null);
        }
        return menuMapper.selectByUserName(name);
    }

    @Override
    public List<SysMenuTreeNode> findTree(String userName, int i) {
        List<SysMenuTreeNode> treeNodes = new ArrayList<>();
        List<SysMenu> byUserName = findByUserName(userName);
        for (SysMenu sysMenu : byUserName) {
            if (sysMenu.getParentId() == null || sysMenu.getParentId() == 0) {
                if (exists(treeNodes, sysMenu)) { continue;}
                SysMenuTreeNode node = new SysMenuTreeNode();
                node.setLevel(0);
                setParam(sysMenu, node);
                treeNodes.add(node);
            }
        }
        treeNodes.sort((o1, o2) -> o1.getOrder().compareTo(o2.getOrder()));
        findChildren(treeNodes, byUserName, i);
        return treeNodes;
    }

    private void findChildren(List<SysMenuTreeNode> treeNodes, List<SysMenu> byUserName, int i) {

        for (SysMenuTreeNode node : treeNodes) {
            List<SysMenuTreeNode> children = new ArrayList<>();
            for (SysMenu menu : byUserName) {
                if (i == 1 && menu.getType() == 2) {
                    continue;
                }
                if (node.getId() != null && node.getId().equals(menu.getParentId())) {
                    if (exists(children, menu)) { continue;}
                    SysMenuTreeNode childrenNode = new SysMenuTreeNode();
                    childrenNode.setLevel(node.getLevel() + 1);
                    childrenNode.setParentName(node.getName());
                    setParam(menu, childrenNode);
                    children.add(childrenNode);
                }
            }
            children.sort((o1, o2) -> o1.getOrder().compareTo(o2.getOrder()));
            node.setChildren(children);
            findChildren(children, byUserName, i);
        }
    }

    private void setParam(SysMenu menu, SysMenuTreeNode childrenNode) {
        childrenNode.setOrder(menu.getOrderNum());
        childrenNode.setName(menu.getName());
        childrenNode.setIcon(menu.getIcon());
        childrenNode.setId(menu.getId());
    }

    private boolean exists(List<SysMenuTreeNode> treeNodes, SysMenu menu) {
        for (SysMenuTreeNode treeNode : treeNodes) {
            if (treeNode.getId().equals(menu.getId())) {
                return true;
            }
        }
        return false;
    }
}
